﻿using System;
using System.Collections.Generic;
using System.Text;
using FrameworkX.TinyCms.Core.Security;
using FrameworkX.TinyCms.Core.Config;

namespace FrameworkX.TinyCms.Core.Service
{
    public static class ServiceManager
    {

        private static Dictionary<Type, IService> _services;
        private static object _lockHandle = new object();

        public static IService AquireService(Type serviceType)
        {
            return _services[serviceType];
        }


        static ServiceManager()
        {
            lock (_lockHandle)
            {
                if (_services != null)
                {
                    _services = new Dictionary<Type, IService>();
                }
            }
            RegisterWellknownServices();
        }

        static void RegisterWellknownServices()
        {
            RegisterService(new SecurityService());
            RegisterService(new SettingService());
            RegisterService(new ContentService());
        }

        public static bool RegisterService(IService service)
        {
            if (!_services.ContainsKey (service.GetType()))
            {
                lock (_lockHandle)
                {
                    _services[service.GetType()] = service;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool UnRegisterService(IService service)
        {
            if (!_services.ContainsKey (service.GetType()))
            {
                lock (_lockHandle)
                {
                    _services.Remove(service.GetType());
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
